<template>
	<view>
		<view class="blogitem">
			<view class="head">
				<view class="userinfo">
					<view class="avatar">
						<image :src="giveAvatar(item)" mode="aspectFill"></image>
					</view>
					<view class="name">
						{{giveName(item)}}
					</view>
					<view class="releasetime">
						<uni-dateformat :date="item.publish_date" :threshold="[60000, 3600000]"
							format="yyyy/MM/dd hh:mm:ss"></uni-dateformat>
					</view>
				</view>
				<view class="more" @click="clickMore">
					<text class="iconfont icon-ellipsis"></text>
				</view>
			</view>

			<view class="middle">
				<view class="articlecontent">
					<view class="title" @click="goDetail">
						{{item.title}}
					</view>
					<view class="text" @click="goDetail">
						<view class="t">
							{{item.description}}
						</view>
					</view>
					<view class="piclist">
						<view class="pic" :class="item.picurls.length==1 ? 'only': ''"
							v-for="(pic,index) in item.picurls" :key="pic">
							<image @click="clickPic(index)" :src="pic" mode="aspectFill"></image>
						</view>
					</view>
				</view>
			</view>

			<view class="foot">
				<view class="actions">
					<view class="box">
						<text class="iconfont icon-a-27-liulan"></text>
						<text>{{item.view_count || '浏览'}}</text>
					</view>
					<view class="box" @click="goDetail">
						<text class="iconfont icon-a-5-xinxi"></text>
						<text>{{item.comment_count || '评论'}}</text>
					</view>
					<view class="box" @click="clickLike">
						<!-- <text class="iconfont icon-a-106-xihuan"></text> -->
						<u-icon class="iconfont" :name="item.isLike ? 'thumb-up-fill' : 'thumb-up'"
							:color="item.isLike ? '#2979ff' : ''" size="26"></u-icon>
						<text>{{item.like_count || '点赞'}}</text>
					</view>
				</view>
			</view>
		</view>

		<view>
			<u-action-sheet :actions="getActionsList" :closeOnClickOverlay="true" :closeOnClickAction="true"
				cancelText="取消" :round="15" :show="sheetShow" @select="sheetSelect" @close="onClose"></u-action-sheet>
		</view>
	</view>
</template>

<script>
	import pageJson from "@/pages.json"
	import {
		giveName,
		giveAvatar,
		likeFun
	} from "@/utils/tools.js"
	import {
		store
	} from '@/uni_modules/uni-id-pages/common/store.js'
	const db = uniCloud.database()
	export default {
		name: "BlogItem",
		props: ['item', 'userID', 'isLike', 'like_count'],
		data() {
			return {
				actionsList: [{
						name: "修改",
						type: "edit",
						color: "#2d8cf0",
						disabled: true
					},
					{
						name: "删除",
						type: "del",
						color: "#F56C6C",
						disabled: true
					}
				],
				emptyActionsList: [],
				sheetShow: false,
			};
		},
		computed: {
			getActionsList() {
				let uid = this.$props.userID
				// let uid = uniCloud.getCurrentUserInfo().uid
				// console.log(uid);
				if (uid == this.item.user_id[0]._id || this.uniIDHasRole('admin') || this.uniIDHasRole('webmaster')) {
					this.actionsList.forEach(item => {
						item.disabled = false
						// console.log(item);
					})
					return this.actionsList
				} else {
					return []
				}
			}
		},
		methods: {
			giveName,
			giveAvatar,
			//点赞操作
			clickLike() {
				if (!store.hasLogin) {
					uni.showModal({
						title: "登录后才可进行后续操作",
						success: res => {
							if (res.confirm) {
								uni.navigateTo({
									url: "/" + pageJson.uniIdRouter.loginPage
								})
							}
						}
					})
					return;
				}

				let time = Date.now();
				if (time - this.likeTime < 2000) {
					uni.showToast({
						title: "操作太频繁，请稍后...",
						icon: "none"
					})
					return;
				}
				this.likeTime = time;

				let like_count = this.item.like_count;
				this.item.isLike ? like_count-- : like_count++
				let isLike = !this.item.isLike;

				this.$emit("update:isLike", isLike)
				this.$emit("update:like_count", like_count)

				likeFun(this.item._id);

			},
			//点击跳转到详情
			goDetail() {
				uni.navigateTo({
					url: "/pages/detail/detail?id=" + this.item._id
				})
			},
			//点击更多操作
			clickMore() {
				if (!this.getActionsList.length) {
					uni.showToast({
						title: '啥都没有',
						icon: "none",
						mask: true,
						duration: 800,
					});
					return
				}

				this.sheetShow = true;
			},
			//点击选择
			sheetSelect(e) {
				this.sheetShow = false
				let type = e.type;
				// console.log(type);
				if (type == "edit") {
					this.editFun();
				}
				if (type == "del") {
					this.delFun();
				}
			},
			editFun() {
				uni.showLoading({
					title: "加载中..."
				})
				db.collection("cloud_fishCircle-article").doc(this.item._id).field("content, picurls, title")
					.get()
					.then(res => {
						// console.log(res);
						uni.navigateTo({
							url: `/pages/edit/edit?metN=update&actID=${this.item._id}`,
							success: (r) => {
								// 通过eventChannel向被打开页面传送数据
								r.eventChannel.emit('acceptDataFromOpenerPage', {
									data: res.result.data[0]
								})
							}
						})
						// console.log(res);
					}).catch(err => {
						uni.showToast({
							title: "获取数据失败",
							icon: "none"
						})
					}).finally(() => {
						uni.hideLoading()
					})
			},
			delFun() {
				uni.showLoading({
					title: "加载中..."
				})
				db.collection("cloud_fishCircle-article").doc(this.item._id).update({
					delState: true
				}).then(res => {
					uni.showToast({
						title: "删除成功",
						icon: "none"
					})
					this.$emit("delEvent", this.item._id)
				}).catch(err => {
					uni.showToast({
						title: "删除失败",
						icon: "none"
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			//取消弹窗
			onClose() {
				this.sheetShow = false
			},
			//单击图片
			clickPic(index) {
				uni.previewImage({
					urls: this.item.picurls,
					current: index
				})
			}
		}
	}
</script>

<style lang="scss">
	.blogitem {
		// border: 1px solid #000;
		padding: 15rpx;

		.head {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 32rpx;

			.userinfo {
				display: flex;
				align-items: center;

				.avatar {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					overflow: hidden;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.name {
					color: #222;
					padding-left: 15rpx;
				}

				.releasetime {
					color: #888;
					font-size: 22rpx;
					padding-left: 15rpx;
				}
			}

			.more {
				padding: 5rpx;

				.iconfont {
					font-size: 50rpx;
					color: #888;
				}
			}
		}

		.middle {
			padding: 15rpx 0 15rpx;

			.articlecontent {

				.title {
					font-size: 44rpx;
					color: #000;
					font-weight: 600;
					text-align: justify;
					@include text-omission(2);
				}

				.text {
					padding-top: 10rpx;
					padding-bottom: 15rpx;
					font-size: 30rpx;
					text-align: justify;
					color: #888;

					.t {
						@include text-omission(2);
					}
				}

				.piclist {
					display: flex;

					.pic {
						width: 220rpx;
						height: 220rpx;
						margin-right: 6rpx;
						overflow: hidden;

						image {
							width: 100%;
							height: 100%;
						}

						&:first-child {
							border-radius: 20rpx 0 0 20rpx;
						}

						&:last-child {
							border-radius: 0 20rpx 20rpx 0;
						}

						&.only {
							border-radius: 20rpx;
						}
					}
				}
			}
		}

		.foot {
			.actions {
				border-top: 1rpx solid #eee;
				display: flex;
				justify-content: space-around;
				font-size: 26rpx;
				color: #333;

				.box {
					height: 70rpx;
					// flex: 1;
					display: flex;
					align-items: center;
					padding: 15rpx 0 0;

					.iconfont {
						font-size: 40rpx;
						padding-right: 10rpx;
					}
				}

				// .box.active {
				// 	color: #0199FE
				// }
			}
		}

	}
</style>